#!/bin/bash

#ideviceinstaller is required to run this script
#it can be installed using the following instructions:
#http://technotesonthego.blogspot.co.uk/2013/10/install-libimobiledevice-on-mac-os-x.html


PROJECT_NAME=OFSApp\ Copy

#PROVISIONING_PROFILE_ID=`grep com.bskyb.skybus /Users/${USER}/Library/MobileDevice/Provisioning\ Profiles/*.mobileprovision | awk -F"/" '{print $NF}' | awk -F "." '{print $1}' | head -n1`

PROVISIONING_PROFILE_ID=92B96C67-119B-4267-BA8A-F9DA45A21B27


IDENTITY="iPhone Distribution: Sky Subscribers Services Limited"

PROJECT_BUILD_DIR=/Users/yha03/Documents/OFSApp/BuildApp

cd "/Users/yha03/Documents/Aptana Studio 3 Workspace/sb-ofs-mobile/src/main/cordova/platforms/ios"


if [ -a ${PROJECT_NAME}.app ]; then
  rm -rf ${PROJECT_NAME}.app
  echo "Removing old app from location"
fi 

if [ ! -d "$PROJECT_BUILD_DIR" ]; then
  mkdir $PROJECT_BUILD_DIR
  chmod 777 -R $PROJECT_BUILD_DIR
fi

echo "Building: ${PROJECT_NAME}"


#xcodebuild -scheme ${PROJECT_NAME} PROVISIONING_PROFILE="${PROVISIONING_PROFILE_ID}" -configuration Release CODE_SIGN_IDENTITY="iPhone Distribution: Sky Subscribers Services Limited" ARCHS="i386 armv7 armv7s arm64" ONLY_ACTIVE_ARCH="NO" CONFIGURATION_BUILD_DIR="${PROJECT_BUILD_DIR}" clean build



#Build the monkey talk version

xcodebuild -target "OFSAppWithMonkeyAgent" PROVISIONING_PROFILE="${PROVISIONING_PROFILE_ID}" -configuration Release CODE_SIGN_IDENTITY="${IDENTITY}" ARCHS="i386 armv7 armv7s arm64" ONLY_ACTIVE_ARCH="NO" CONFIGURATION_BUILD_DIR="${PROJECT_BUILD_DIR}" clean build


#cp -r ${PROJECT_BUILD_DIR}/${PROJECT_NAME}.app ./

#ideviceinstaller --install ${PROJECT_NAME}.app

cp -r ${PROJECT_BUILD_DIR}/OFSApp\ Copy.app ./

ideviceinstaller --install OFSApp\ Copy.app


#rm -rf $PROJECT_BUILD_DIR
echo "Completed installing the app on device..."

